#include "graphics.h"
#include "log.h"
#include <cstdio>


using namespace std;
// lock screen surface
void Graphics::slock() {
  if(SDL_MUSTLOCK(screen))  {
    if(SDL_LockSurface(screen) < 0) {
      return;
    }
  }
}
// unlock screen surface
void Graphics::sulock() {
  if(SDL_MUSTLOCK(screen)) {
    SDL_UnlockSurface(screen);
  }
}

// next 4 functions suxx
int Graphics::InitImages() {
  back = SDL_LoadBMP("bg.bmp");
  image = SDL_LoadBMP("mech.bmp");
  return 0;
}
void Graphics::DrawIMG(SDL_Surface *img, int x, int y) {
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}



void Graphics::DrawBG() {
  slock();
  DrawIMG(back, 0, 0);
  sulock();
}
void Graphics::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2) {
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_Rect dest2;
  dest2.x = x2;
  dest2.y = y2;
  dest2.w = w;
  dest2.h = h;
  SDL_BlitSurface(img, &dest2, screen, &dest);
}


void Graphics::init(Client *c) {
	_client = c;
};

int Graphics::init_screen() {
  #if defined(WIN32) 
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {  
      *lg << error << "Unable to init SDL with (SDL_INIT_AUDIO|SDL_INIT_VIDEO): " << SDL_GetError() << fl;
      return 0;
  }
  #else
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_EVENTTHREAD) < 0 ) {  
      *lg << error << "Unable to init SDL with (SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_EVENTTHREAD): " << SDL_GetError() << fl;
      return 0;
  }
  #endif
  atexit(SDL_Quit); 
    *lg << info << "SDL_Init" << fl; 
//      screen = SDL_SetVideoMode(1152, 768, 32,  SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN); 
      screen = SDL_SetVideoMode(640, 480, 32,  SDL_HWSURFACE|SDL_DOUBLEBUF);      

  if ( screen == NULL ) {
    *lg << error << "Unable to set HW/DBF video mode 640x480 " << SDL_GetError() << fl;
     screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
     if ( screen == NULL ) {
       *lg << error << "Unable to set SW video mode 640x480 " << SDL_GetError() << fl;
        return 0;
    }
  }
  *lg << info << "SDL_SetVideoMode" << fl;

  SDL_ShowCursor(SDL_DISABLE);

//	gamma = 1.0 + (option_get_value(OPT_BRIGHTNESS) * 0.1);

//	SDL_SetGamma(gamma, gamma, gamma);

  return 1;
}
int Graphics::init_input() {
  SDL_EnableKeyRepeat(1, 1);
  SDL_EnableUNICODE(1);
  return 1;
}
void Graphics::main() {
  if(!init_screen() || !init_input()) {
    return;
  }
  Uint8* keys;


  int done=0;

  InitImages();
  DrawBG();
  alpha = 1;

  while(done == 0)
  {

// TODO: resize
/*case SDL_VIDEORESIZE:
			SetNewVideoMode(event.resize.w, event.resize.h, MainScreen->format->BitsPerPixel);
			break;*/
    // keyboard handling
    SDL_Event event;
char text[100];         
    while ( SDL_PollEvent(&event) ) {
		if ( event.type == SDL_QUIT )  {  done = 1;  }

      if ( event.type == SDL_KEYDOWN )
      {
        keys = SDL_GetKeyState(NULL);                                             
        if ( keys[SDLK_ESCAPE] ) { done = 1; }
        if ( keys[SDLK_UP] ) { ypos -= 1; }
        if ( keys[SDLK_DOWN] ) { ypos += 1; }
        if ( keys[SDLK_LEFT] ) { xpos -= 1; }
        if ( keys[SDLK_RIGHT] ) { xpos += 1; }      

if(event.key.keysym.unicode) {
*lg << event.key.keysym.unicode << fl;
sprintf(text,"%s%c",text, event.key.keysym.unicode);
*lg << text << fl;
}
      }
    }
    
       
    slock();

    DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
    DrawIMG(image, xpos, ypos);

    char buff[100]; 
    sprintf(buff, "Client identifier : %d" , _client->_client_identifier);
    filledCircleRGBA(screen, 200, 200, 50, 255, 0, 0, alpha);
    filledCircleRGBA(screen, 245, 275, 50, 0, 255, 0, alpha);
    filledCircleRGBA(screen, 275, 200, 50, 0, 0, 255, alpha);
    stringRGBA(screen, 450, 3, buff, 255, 255, 255, 150-alpha);
    stringRGBA(screen, 450, 23, text, 255, 255, 255, 150-alpha);

    SDL_Flip(screen);
    
    sulock();
    SDL_Delay(2); // piecka na procak (bez toho alebo len delay(1) to zeria prilis vela)
  }
}

Graphics::Graphics() {
};

Graphics::~Graphics() {
  	_client->put("/quit");
    SDL_Quit();
};


